package cn.tworice.blog.task;

import cn.tworice.blog.service.ToolsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;

@Component          // IOC容器
@EnableScheduling   // 1.开启定时任务
@EnableAsync        // 2.开启多线程
@Slf4j              // 日志
public class MultithreadScheduleTask {

    @Value("${blog.QRCodeFile}")
    String qrCodePath;

    @Autowired
    private ToolsService toolsService;

    /**
     * 异步清理二维码图片
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @Async
    @Scheduled(cron = "0 0 4 * * ?")  // 每天凌晨4点执行清空二维码图片
    public void clearQRCodes(){
        try {
            log.info("执行清理二维码图片");
            File file = new File(this.qrCodePath);
            if(file.isDirectory()){
                File[] files = file.listFiles();
                for(File f:files){
                    f.delete();
                }
            }
            log.info("执行清理二维码图片成功");
        }catch (Exception ex){
            ex.printStackTrace();
            log.error("执行清理二维码图片失败-"+ex.getMessage());
        }
    }

    /**
     * Q校园签到
     * @author 二饭
     */
    @Async
    @Scheduled(cron = "0 0 18 * * ?")  // 每天下午6点执行上报体温
    public void qSchoolSignin(){
        try {
            log.info("Q校园签到");
            String s = toolsService.signIn();
            log.info(s);
        }catch (Exception ex){
            ex.printStackTrace();
            log.error("Q校园签到失败-"+ex.getMessage());
        }
    }
}
